<html>
<head>
  <title>main.py</title>
</head>
<body bgcolor="#ffffff" text="#000000">
<pre>
<font color="#444444">#!/usr/bin/env python</font>

<font color="#444444"># -------------------------------</font>
<font color="#444444"># projects/python/collatz/main.py</font>
<font color="#444444"># Copyright (C) 2009</font>
<font color="#444444"># Glenn P. Downing</font>
<font color="#444444"># -------------------------------</font>

<font color="#444444"># To run the program</font>
<font color="#444444">#     main.py &lt; Collatz.in &gt; Collatz.out</font>

<font color="#444444"># To document the program</font>
<font color="#444444">#     pydoc -w main</font>

<font color="#444444"># -------</font>
<font color="#444444"># globals</font>
<font color="#444444"># -------</font>

<font color="#2040a0">i</font> <font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="#444444"># input: don't change after reading</font>
<font color="#2040a0">j</font> <font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="#444444"># input: don't change after reading</font>
<font color="#2040a0">v</font> <font color="4444FF">=</font> <font color="#FF0000">0</font> <font color="#444444"># output</font>

<font color="#444444"># -----------</font>
<font color="#444444"># InputReader</font>
<font color="#444444"># -----------</font>

<strong>class</strong> <font color="#2040a0">InputReader</font> <font color="4444FF">(</font><font color="#2040a0">object</font><font color="4444FF">)</font> <font color="4444FF">:</font>
<strong>   <font color="ff0000"> def</font><font color="ff0000"> read</font> <font color="2040a0">(self)</font> :
</strong>        <strong>return</strong> <font color="#2040a0">raw_input</font><font color="4444FF">(</font><font color="4444FF">)</font>

<font color="#444444"># ------------</font>
<font color="#444444"># OutputWriter</font>
<font color="#444444"># ------------</font>

<strong>class</strong> <font color="#2040a0">OutputWriter</font> <font color="4444FF">(</font><font color="#2040a0">object</font><font color="4444FF">)</font> <font color="4444FF">:</font>
<strong>   <font color="ff0000"> def</font><font color="ff0000"> write</font> <font color="2040a0">(self, *a)</font> :
</strong>        <strong>for</strong> <font color="#2040a0">w</font> <strong>in</strong> <font color="#2040a0">a</font> <font color="4444FF">:</font>
            <strong>print</strong> <font color="#2040a0">w</font>,
        <strong>print</strong>

<font color="#444444"># -------</font>
<font color="#444444"># my_read</font>
<font color="#444444"># -------</font>

<font color="#2040a0">def</font> <font color="#2040a0">my_read</font> <font color="4444FF">(</font><font color="#2040a0">r</font><font color="4444FF">)</font> <font color="4444FF">:</font>
    <font color="#008000">&quot;&quot;</font><font color="#008000">&quot;
    reads an int into i and j
    return true if that succeeds, false otherwise
    &quot;</font><font color="#008000">&quot;&quot;</font>
    <strong>global</strong> <font color="#2040a0">i</font>
    <strong>global</strong> <font color="#2040a0">j</font>
    <strong>try</strong> <font color="4444FF">:</font>
        <font color="#2040a0">s</font> <font color="4444FF">=</font> <font color="#2040a0">r</font>.<font color="#2040a0">read</font><font color="4444FF">(</font><font color="4444FF">)</font>
    <strong>except</strong> <font color="#2040a0">EOFError</font> <font color="4444FF">:</font>
        <strong>return</strong> <font color="#2040a0">False</font>
    <font color="#2040a0">l</font> <font color="4444FF">=</font> <font color="#2040a0">s</font>.<font color="#2040a0">split</font><font color="4444FF">(</font><font color="4444FF">)</font>
    <font color="#2040a0">i</font> <font color="4444FF">=</font> <font color="#2040a0">int</font><font color="4444FF">(</font><font color="#2040a0">l</font><font color="4444FF">[</font><font color="#FF0000">0</font><font color="4444FF">]</font><font color="4444FF">)</font>
    <font color="#2040a0">j</font> <font color="4444FF">=</font> <font color="#2040a0">int</font><font color="4444FF">(</font><font color="#2040a0">l</font><font color="4444FF">[</font><font color="#FF0000">1</font><font color="4444FF">]</font><font color="4444FF">)</font>
    <strong>return</strong> <font color="#2040a0">True</font>

<font color="#444444"># -------</font>
<font color="#444444"># my_eval</font>
<font color="#444444"># -------</font>

<font color="#2040a0">def</font> <font color="#2040a0">my_eval</font> <font color="4444FF">(</font><font color="4444FF">)</font> <font color="4444FF">:</font>
    <font color="#008000">&quot;&quot;</font><font color="#008000">&quot;
    computes the max cycle length in the range [i, j]
    and stores the result in v
    &quot;</font><font color="#008000">&quot;&quot;</font>
    <strong>global</strong> <font color="#2040a0">v</font>
    <font color="#2040a0">v</font> <font color="4444FF">=</font> <font color="#FF0000">0</font>

<font color="#444444"># --------</font>
<font color="#444444"># my_print</font>
<font color="#444444"># --------</font>

<font color="#2040a0">def</font> <font color="#2040a0">my_print</font> <font color="4444FF">(</font><font color="#2040a0">w</font><font color="4444FF">)</font> <font color="4444FF">:</font>
    <font color="#008000">&quot;&quot;</font><font color="#008000">&quot;
    writes the values of i, j, and v
    &quot;</font><font color="#008000">&quot;&quot;</font>
    <font color="#2040a0">w</font>.<font color="#2040a0">write</font><font color="4444FF">(</font><font color="#2040a0">i</font>, <font color="#2040a0">j</font>, <font color="#2040a0">v</font><font color="4444FF">)</font>

<font color="#444444"># ----</font>
<font color="#444444"># main</font>
<font color="#444444"># ----</font>

<font color="#2040a0">def</font> <font color="#2040a0">main</font> <font color="4444FF">(</font><font color="4444FF">)</font> <font color="4444FF">:</font>
    <font color="#008000">&quot;&quot;</font><font color="#008000">&quot;
    runs the program
    &quot;</font><font color="#008000">&quot;&quot;</font>
    <strong>while</strong> <font color="#2040a0">my_read</font><font color="4444FF">(</font><font color="#2040a0">InputReader</font><font color="4444FF">(</font><font color="4444FF">)</font><font color="4444FF">)</font> <font color="4444FF">:</font>
        <font color="#2040a0">my_eval</font><font color="4444FF">(</font><font color="4444FF">)</font>
        <font color="#2040a0">my_print</font><font color="4444FF">(</font><font color="#2040a0">OutputWriter</font><font color="4444FF">(</font><font color="4444FF">)</font><font color="4444FF">)</font>

<strong>if</strong> <font color="#2040a0">__name__</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#008000">&quot;__main__&quot;</font> <font color="4444FF">:</font>
    <font color="#2040a0">main</font><font color="4444FF">(</font><font color="4444FF">)</font>

</pre>
<hr>
syntax highlighted by <a href="http://www.palfrader.org/code2html">Code2HTML</a>, v. 0.9.1
</body>
</html>
